package com.zretc.service.impl;

import com.zretc.dao.ChatMapper;
import com.zretc.entity.Message;
import com.zretc.service.ChatService;
import com.zretc.util.RBMQReceiver;
import com.zretc.util.RBMQSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2020/8/22.
 */
@Service
@Component
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private  static ConcurrentHashMap<String,RBMQSender> senderlist = new ConcurrentHashMap<String,RBMQSender>();
    private  static ConcurrentHashMap<String,RBMQReceiver> receivelist = new ConcurrentHashMap<String,RBMQReceiver>();
    private  static List<Message> list = new ArrayList<>();

    private static ChatServiceImpl cs;

    @PostConstruct
    public void init(){
        cs = this;
    }
    @Override
    public void getChatmanAndInit(String User) {
        System.out.println("aaa");
        System.out.println(User);
      RBMQSender rbmqSender  = new RBMQSender();
      RBMQReceiver rbmqReceiver  = new RBMQReceiver();
        ChatServiceImpl.senderlist.put(User,rbmqSender);
        ChatServiceImpl.receivelist.put(User,rbmqReceiver);
        System.out.println( rbmqSender);
        System.out.println( rbmqReceiver);
    }

    @Override
    public void sendAndSaveMsessage(String sendUser,String receiveUser,String msg) {
        Message msgInfo = new Message();
        msgInfo.setSenderId(sendUser);
        msgInfo.setReceiverId(receiveUser);
        msgInfo.setMsgContent(msg);
        ChatServiceImpl.list.add(msgInfo);
        cs.redisTemplate.opsForList().leftPush("list",this.list);
        RBMQSender rs = ChatServiceImpl.senderlist.get(sendUser);
        rs.setSenderId(sendUser);
        rs.send(receiveUser,msg);
    }

    @Override
    public void getAndReceiveMsessage(String receiveUser) {
        RBMQReceiver rr = ChatServiceImpl.receivelist.get(receiveUser);
    }

    @Override
    public List<String> findUserByCondition(String userId) {
        if(userId == "" ){
             userId = null;
        }
        return chatMapper.findUserByCondition(userId);
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void timer(){
        //定时查找消息列表并遍历持久化
        List<Message> list = new ArrayList<>();
        list= (List<Message>) cs.redisTemplate.opsForList().leftPop("list");
        System.out.println("list");
        System.out.println(list);
        if(list!=null) {
            for (Message msg : list) {
                chatMapper.insertMsg(msg);
            }
        }
    }
}
